<?php


include_once('Month.php');
include_once('dbinfo.php');
include_once('dbDates.php');

/**
 * Drops the dbMonth table if it exists, and creates a new one
 * Table fields:
 * [0] id: mm-dd-yy
 * [1] dates: array of RMHdates
 * [2] name: name of Month ie March 2008
 * [3] weekday_start: weekday for first day, 0=Sunday
 * [4] days: number of days in the month
 * [5] timestamp: timestamp for the first day of the month
 */
function setup_dbMonth() {
   connect();
   mysql_query("DROP TABLE IF EXISTS dbMonth");
   $result=mysql_query("CREATE TABLE dbMonth (id CHAR(10) NOT NULL, dates INT,
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  name TEXT, weekday_start INT, days INT, timestam INT, PRIMARY KEY (id))");

﻿  if(!$result)
﻿  ﻿  echo mysql_error();
﻿  mysql_close();
}

/**
 * Inserts a month into the db
 * @param $m the month to insert
 */
function insert_dbMonth($m) {
   if (! $m instanceof Month) {
﻿  ﻿  die ("Invalid argument for dbMonth->add_month function call");
﻿  }
   connect();
   $query = "SELECT * FROM dbMonth WHERE id =\"".$w->get_id()."\"";
   $result = mysql_query ($query);
   if(mysql_num_rows($result)!=0) {
   ﻿  ﻿  delete_dbMonth($m);
   ﻿  ﻿  connect();
   }
   $query="INSERT INTO dbMonth VALUES
﻿  ﻿  ﻿  ﻿  (\"".$w->get_id()."\",".get_dates_text($w->get_dates()).",\"".
﻿  ﻿  ﻿  ﻿  $w->get_dates()."\",\"".
﻿  ﻿  ﻿  ﻿  $w->get_name()."\",\"".
﻿  ﻿  ﻿  ﻿  $w->get_weekday_start()."\",\"".
﻿  ﻿  ﻿  ﻿  $w->get_days()."\",\"".
﻿  ﻿  ﻿  ﻿  $w->get_timestamp()."\",\"".

   $result=mysql_query($query);
   mysql_close();
   if (!$result) {
﻿  ﻿  echo ("unable to insert into dbMonth: ".$w->get_id(). mysql_error());
﻿  ﻿  return false;
   }
   else foreach($w->get_dates() as $i)
﻿  ﻿  insert_dbDates($i);
   return true;
 }

/**
 * Deletes a month from the db
 * @param $m the month to delete
 */
function delete_dbMonth($m) {
﻿  if (! $m instanceof Month)
﻿  ﻿  die ("Invalid argument for delete_dbMonth function call");
﻿  connect();
    $query="DELETE FROM dbMonth WHERE id=\"".$w->get_id()."\"";
﻿  $result=mysql_query($query);
﻿  mysql_close();
﻿  if (!$result) {
﻿  ﻿  echo ("unable to delete from dbMonth: ".$w->get_id(). mysql_error());
﻿  ﻿  return false;
﻿  }
    else foreach ($w->get_dates() as $i)
﻿  ﻿  delete_dbDates($i);
﻿  return true;
 }

/**
 * Updates a month in the db by deleting it and re-inserting it
 * @param $m the month to update
 */
function update_dbMonth($m) {
﻿  if (! $w instanceof Month)
﻿  ﻿  die ("Invalid argument for dbMonth->replace_week function call");
﻿  if (delete_dbMonth($m))
﻿     return insert_dbMonth($m);
﻿  else return false;
}

/**
 * Selects a month from the database
 * @param $id month id
 * @return mysql entry corresponding to id
 */
function select_dbMonth($id) {
﻿  if(strlen($id)!=8) {
﻿  ﻿  die ("Invalid month id.");
﻿  }
﻿  else {
﻿  ﻿  $timestamp = mktime(0,0,0,substr($id,0,2),substr($id,3,2),substr($id,6,2));
 ﻿  ﻿  $dow = date("N",$timestamp);
﻿  ﻿  $id=date("m-d-y",mktime(0, 0, 0, substr($id,0,2), substr($id,3,2)-$dow+1, substr($id,6,2)));
﻿  }
﻿  connect();
    $query = "SELECT * FROM dbMonth WHERE id =\"".$id."\"";
    $result = mysql_query ($query);
﻿  if (!$result) {
﻿      echo 'Could not run query: ' . mysql_error();
﻿  ﻿  $result_row = false;
﻿  }
﻿  else
﻿  ﻿  $result_row=mysql_fetch_row($result);
﻿  mysql_close();
﻿  return $result_row;
}

/**
 * retrieves a Month from the database
 * @param $id = id of the week to retrieve
 * @return the desired month
function get_dbMonth($id) {
﻿  $result_row=select_dbMonth($id);
﻿  if($result_row) {
﻿  ﻿  $dates=$result_row[1];
﻿  ﻿  $dates=explode("*",$dates);
﻿  ﻿  $d=array();
﻿  ﻿  foreach($dates as $i){
﻿  ﻿  ﻿  $d[]=select_dbDates($i);
﻿  ﻿  }
﻿  ﻿  $m=new Month($d, $result_row[2], $result_row[3], $result_row[4], $result_row[5]);
﻿  }
﻿  return $m;
}

/**
 * the full contents of dbMonth, used by addMonth to list all scheduled months
 * @return mysql result array of months
 */
function get_all_dbMonth() {
﻿  connect();
﻿  $query="SELECT * FROM dbMonth";
﻿  $result=mysql_query($query);
﻿  mysql_close();
﻿  return $result;
}

/**
 * generates a string of date ids
 * @param $dates array of dates for a month
 * @return string of date ids, * delimited
 */
function get_dates_text($dates){
﻿  $d="\"".$dates[0]->get_id();
﻿  for($i=1;$i<7;++$i) {
﻿  ﻿  $d=$d."*".$dates[$i]->get_id();
﻿  }
﻿  return $d."\"";
}

?>
